using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AspNetForums;
using AspNetForums.Components;

namespace AspNetForums.Admin
{
	/// <summary>
	/// Summary description for RoleAdmin.
	/// </summary>
	public class RoleAdmin : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Literal RoleID;
		protected System.Web.UI.WebControls.TextBox Name;
		protected System.Web.UI.WebControls.TextBox Description;
		protected System.Web.UI.WebControls.Button Save;
	
		protected Role _roleToEdit;

		private void Page_Load(object sender, System.EventArgs e)
		{
//			int roleID = Convert.ToInt32(HttpContext.Current.Request.QueryString["RoleId"]);
			int roleID = ForumContext.Current.RoleID;

			// roles 0-4 are "hard coded" in the database and should not be edited
			if (roleID <= 4) {
				return;
			}

			_roleToEdit = Roles.GetRole(roleID);
			RoleID.Text		= _roleToEdit.RoleID.ToString();
			Name.Text			= _roleToEdit.Name.ToString();
			Description.Text	= _roleToEdit.Description.ToString();

			// Wire-up buttons
			Save.Click	+= new EventHandler(Save_Click);

			if( !Page.IsPostBack )
				DataBind();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Save_Click(object sender, EventArgs e) {

			_roleToEdit.RoleID		= Convert.ToInt32(HttpContext.Current.Request.QueryString["RoleId"]);
			_roleToEdit.Name        = Name.Text;
			_roleToEdit.Description = Description.Text;

			Roles.UpdateRole( _roleToEdit);

			// save is complete, update the page data
			DataBind();
		}
	}
}
